package org.dq.base.page;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dq.base.page.impl.PageImpl;
import org.springframework.core.convert.converter.Converter;

public class InterfaceFactory {
	
	
	/**
	 * 转换数据,将Page对象中List集合中元素数据转化为其它数据类型
	 * @param converter 数据转换�?
	 */
	public <T> Page convertData(Converter<Object,T> converter,Page page){
		try {
			List<T> list=new ArrayList<T>();
			List<?> recList=page.getRecList();
			for(Object source:recList){
				list.add(converter.convert(source));
			}
			
			Field recListField=PageImpl.class.getField("recList");
			recListField.setAccessible(true);
			recListField.set(page, list);
			
			return page;
			
		} catch( Exception e) {
			throw new PageException(e);
		} 
	}
	
	public static Map<String,Object> layuiData(Page page){
		
		Map<String,Object> map=new HashMap<String,Object>();

		map.put("code",0);
		map.put("msg","");
		map.put("count",page.getTotal());
		map.put("data",page.getRecList());
		
		return  map;
		
	}
	
	public static Map<String,Object> easyUIData(Page page){
		
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("total", page.getTotal());
		map.put("rows", page.getRecList());
		return  map;
		
	}
	
	public static <T> BootStrapTableJsonInterface<T> bootStrapTableData(Class<T> clazz,Page page){
		return new BootStrapTableJsonInterface<T>(){

			@Override
			public int getTotal() {
				return page.getTotal();
			}

			@SuppressWarnings("unchecked")
			@Override
			public List<T> getRows() {
				return (List<T>)page.getRecList();
			}
			
		};
		
	}
	
	public static <T> JqGridJsonInterface<T> jqGridData(Class<T> clazz,Page page) {
		
		return new JqGridJsonInterface<T>(){

			@Override
			public int getPage() {				
				return page.getCurr();
			}

			@Override
			public int getTotal() {//总页�?
				// TODO Auto-generated method stub
				return page.getPgTotal();
			}

			@Override
			public int getRecords() {//总页�?
				// TODO Auto-generated method stub
				return page.getTotal();
			}

			@SuppressWarnings("unchecked")
			@Override
			public List<T> getRows() {
				return (List<T>)page.getRecList();
			}
			
		};
		
		
	}
	
	

}
